package com.tongxin.van.frameworkcanary.http.cookies;

import android.util.Log;

import com.blankj.utilcode.util.SPUtils;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:ReceivedCookiesInterceptor
 *
**/

public class ReceivedCookiesInterceptor implements Interceptor {
    private static final String TAG = "SmartHttp";
    @Override
    public Response intercept(Chain chain) throws IOException {
        Log.d(TAG,"ReceivedCookiesInterceptor start");
        Response originalResponse = chain.proceed(chain.request());
        //正常保存cookie方式
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            HashSet<String> cookies = new HashSet<>();

            for (String header : originalResponse.headers("Set-Cookie")) {
                cookies.add(header);
            }
            SPUtils.getInstance().put("cookies",cookies);
        }

        //能联网保存cookie方式
//        StringBuffer stringBuffer = new StringBuffer();
//        for(String header : originalResponse.headers("Set-Cookie")){
//            stringBuffer.append(header).append(";");
//        }
//        if(!TextUtils.isEmpty(stringBuffer.toString()) && stringBuffer.toString().contains("session.id")){
//            SPUtils.getInstance().put("cookies",stringBuffer.toString());
//        }
        Log.d(TAG,"ReceivedCookiesInterceptor end");
        return originalResponse;
    }
}